﻿#ifndef BASEMODBUS_H
#define BASEMODBUS_H
#include <QObject>
#include "myhead.h"
#include "baseitem.h"
#include "basecommunication.h"

#include "../libmodbus/modbus.h"


class BaseModbus : public BaseCommunication
{
    Q_OBJECT
public:
    Q_INVOKABLE  explicit BaseModbus(QObject *parent = nullptr);
    ~BaseModbus();

    QList<uint16_t> readRegisters(int addr,int count);
    uint16_t readRegister(int addr);

    QList<uint16_t> readInputRegisters(int addr,int count);
    uint16_t readInputRegister(int addr);

    bool writeRegisters(int addr, int count, QList<uint16_t> inputList);
    bool writeRegister(int addr, uint16_t inputList);

    QList<bool> readBits(int addr,int count);
    bool readBit(int addr);

    QList<bool> readInputBits(int addr,int count);
    bool readInputBit(int addr);

    bool writeBits(int addr, int count, QList<bool> coilStates);
    bool writeBit(int addr, bool coilState);

signals:

public slots:

    // BaseCommunicationZZZ interface
public:
    virtual bool connectToHost() override;
    virtual bool isConnected() override;
    virtual bool disConnect() override;

public:
    template<typename T>
    static void pointToList(T *source,int size, QList<T> &target)
    {
        for (int i = 0; i < size; i++)
        {
            target.append(source[i]); // 逐个添加元素到 QList 中
        }
    }

private:
    QMutex  _mutex;   //可以是私有成员变量，也可以是全局变量

protected:
    modbus_t *m_ctx;
};



#endif // BASEMODBUSZZZ_H
